<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="课程" prop="courseId">
        <el-select v-model="dataForm.courseId" placeholder="请选择题所属课程">
          <el-option
              v-for="item in courseList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="考试名" prop="title">
        <el-input v-model="dataForm.title" placeholder="考试名"></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
            value-format="yyyy-MM-dd hh:MM:ss"
            v-model="dataForm.startTime"
            type="datetime"
            placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
            value-format="yyyy-MM-dd hh:MM:ss"
            v-model="dataForm.endTime"
            type="datetime"
            placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getObj, addObj, putObj} from '@/api/teacher/exam'
import {fetchCourseList} from '@/api/teacher/course'

export default {
  data() {
    return {
      courseList: [],
      visible: false,
      canSubmit: false,
      dataForm: {
        id: 0,
        title: '',
        startTime: '',
        endTime: '',
        courseId: ''
      },
      dataRule: {
        title: [
          {required: true, message: '考试名不能为空', trigger: 'blur'}
        ],
        userId: [
          {required: true, message: '创建人ID不能为空', trigger: 'blur'}
        ],
        startTime: [
          {required: true, message: '开始时间不能为空', trigger: 'blur'}
        ],
        endTime: [
          {required: true, message: '结束时间不能为空', trigger: 'blur'}
        ],
        fullScore: [
          {required: true, message: '满分不能为空', trigger: 'blur'}
        ],
        createTime: [
          {required: true, message: '创建时间不能为空', trigger: 'blur'}
        ],
        updateTime: [
          {required: true, message: '更新时间不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.getCourseList()
  },
  methods: {
    getCourseList(){
      fetchCourseList().then(response => {
        this.courseList = response.data
      })
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getObj(this.dataForm.id).then(response => {
            this.dataForm = response.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      console.log()
      console.log(this.dataForm.endTime)
      const start = new Date(this.dataForm.startTime);
      const end = new Date(this.dataForm.endTime);
      if (start > end) {
        this.$notify.warning('考试开始时间不能大于结束时间！')
        return
      }
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    }
  }
}
</script>
